package com.koo.modules.cx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyDateUtil;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.framework.query.MyBatisPlusUtil;
import com.koo.modules.cx.dao.CxProductDao;
import com.koo.modules.cx.entity.CxOrderOutEntity;
import com.koo.modules.cx.entity.CxProductEntity;
import com.koo.modules.cx.service.CxProductService;
import com.koo.modules.quartz.constant.B2BConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
public class CxProductServiceImpl extends ServiceImpl<CxProductDao, CxProductEntity> implements CxProductService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Object key = params.get("key");
        LambdaQueryWrapper<CxProductEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(MyStrUtil.isNotEmpty(key), CxProductEntity::getProductName, key)
                .or().eq(MyStrUtil.isNotEmpty(key), CxProductEntity::getPropertyId, key);

        IPage<CxProductEntity> page = this.page(
                new Query<CxProductEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Integer updateSaleToProduct(List<CxOrderOutEntity> orderOuts) {
        Set<String> productCodes = new HashSet<>();
        List<CxProductEntity> insertProducts = new ArrayList<>();
        for (CxOrderOutEntity orderOut : orderOuts){
            if(!productCodes.contains(orderOut.getProductCode())){
                productCodes.add(orderOut.getProductCode());
                Integer count = baseMapper.selectCount(MyBatisPlusUtil.createEQWrapper(new Object[]{orderOut.getProductCode()}, "productCode"));
                if(MyStrUtil.isEmpty(count) || Integer.valueOf(0).equals(count)) {
                    CxProductEntity baseProduct = new CxProductEntity();
                    BeanUtils.copyProperties(orderOut, baseProduct, "expiryDate");
                    baseProduct.setTradePrice(orderOut.getPurchasePrice());
                    changeProduct(baseProduct);
                    insertProducts.add(baseProduct);
                }
            }
        }
        Integer maxInsertSize = B2BConstant.MAX_INSERT_SIZE.getNumber();
        Integer listSize = insertProducts.size();
        if(listSize > maxInsertSize){
            for(int i = 0; i < listSize; i += maxInsertSize){
                List<CxProductEntity> subList = null;
                if(i + maxInsertSize > listSize){
                    subList = insertProducts.subList(i, listSize);
                }else{
                    subList = insertProducts.subList(i, i + maxInsertSize);
                }
                this.saveBatch(subList);
            }
        } else if (!insertProducts.isEmpty()) {
            return this.saveBatch(insertProducts) ? 1 : 0;
        }
        return 1;
    }

    /**
     * 商品信息补充
     * @param product
     */
    private void changeProduct(CxProductEntity product){
        Date createDate = MyDateUtil.getCurrDateTime();
        product.setCreateTime(createDate);
        product.setUpdateTime(createDate);
        product.setRetailPrice(product.getTradePrice());
        BigDecimal localPrice = product.getTradePrice().multiply(new BigDecimal(2));
        product.setLocalRetailPrice(localPrice);
        product.setMemberPrice(localPrice);
        product.setLocalBarCode(product.getBarCode());
    }

}